package game.games.rpg;

import game.gfx.ColoredChar;
import game.gfx.ConsoleColor;

import java.awt.Point;

import static game.unicode.BoxDrawing.*;

public class CharBufferUtil
{
    public static ColoredChar[][] generate2DCharBuffer(int x, int y, char defaultValue, ConsoleColor defaultColor)
    {
        ColoredChar[][] buffer = new ColoredChar[y][x];
        for (int yy = 0; yy < y; yy++)
        {
            for (int xx = 0; xx < x; xx++)
            {
                buffer[yy][xx] = new ColoredChar(defaultValue, defaultColor);
            }
        }
        return buffer;
    }
    
    public static void fill(ColoredChar[][] charBuffer, Point first, Point last, char fillChar, ConsoleColor color)
    {
        // vertical
        for (int y = first.y; y <= last.y; y++)
        {
            // horizontal
            for (int x = first.x; x <= last.x; x++)
            {
                charBuffer[y][x].setChar(fillChar);
                charBuffer[y][x].setConsoleColor(color);
            }
        }
    }
    
    public static void createDoubleLinedBorder(ColoredChar[][] charBuffer, Point first, Point last, ConsoleColor color)
    {
        // horizontal
        fill(charBuffer, new Point(first.x, first.y), new Point(last.x, first.y), DOUBLE_H_LINE, color);
        fill(charBuffer, new Point(first.x, last.y),  new Point(last.x, last.y),  DOUBLE_H_LINE, color);
        // vertical
        fill(charBuffer, new Point(first.x, first.y), new Point(first.x, last.y), DOUBLE_V_LINE, color);
        fill(charBuffer, new Point(last.x,  first.y), new Point(last.x,  last.y), DOUBLE_V_LINE, color);
        for (int y = first.y; y < last.y; y++)
        {
            charBuffer[y][first.x].setChar(DOUBLE_V_LINE);
            charBuffer[y][last.x].setChar(DOUBLE_V_LINE);
        }
        // border
        charBuffer[first.y][first.x].setChar(DOUBLE_CORNER_UP_LEFT);
        charBuffer[first.y][last.x ].setChar(DOUBLE_CORNER_UP_RIGHT);
        charBuffer[last.y ][first.x].setChar(DOUBLE_CORNER_DOWN_LEFT);
        charBuffer[last.y ][last.x ].setChar(DOUBLE_CORNER_DOWN_RIGHT); 
    }
    
    public static void setString(ColoredChar[] charBuffer, int start, String message, ConsoleColor color)
    {
        if (message.length() > charBuffer.length - start)
        {
            throw new IllegalArgumentException("message to long, max " + (charBuffer.length - start));
        }
        for (int x = 0; x < message.length(); x++)
        {
            charBuffer[start + x].setChar(message.charAt(x));
            charBuffer[start + x].setConsoleColor(color);
        }
        if (message.length() < charBuffer.length)
        {
            for (int x = message.length() + start; x < charBuffer.length; x++)
            {
                charBuffer[x].setChar(' ');
                charBuffer[x].setConsoleColor(ConsoleColor.DEFAULT);
            }
        }
    }
    
    public static void fillString(ColoredChar[] charBuffer, int start, String message, ConsoleColor color)
    {
        StringBuilder buffer = new StringBuilder(message);
        while (buffer.length() < charBuffer.length - start)
        {
            buffer.append(' ');
        }
        setString(charBuffer, start, buffer.toString(), color);
    }
    
    public static ColoredChar[] initCharBuffer(ColoredChar[] charBuffer, char c, ConsoleColor color)
    {
        for (int i = 0; i < charBuffer.length; i++)
        {
            charBuffer[i] = new ColoredChar(c, color);
        }
        return charBuffer;
    }
}
